package com.jingyanzi.marketing.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.AccountDiscountMapper;
import com.jingyanzi.marketing.domain.AccountDiscountDo;
import com.jingyanzi.marketing.entity.MaAccountDiscount;
import com.jingyanzi.marketing.entity.MaAccountDiscountExample;
import com.jingyanzi.marketing.entity.MaAccountDiscountExample.Criteria;
import com.jingyanzi.marketing.query.AccountDiscountQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IAccountDiscountService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maAccountDiscountService")
public class AccountDiscountServiceImpl extends BaseServiceImpl implements IAccountDiscountService {

	@Autowired
	private AccountDiscountMapper accountDiscountMapper;

	public ResultDO add(AccountDiscountDo maAccountDiscountDo) {

		MaAccountDiscount maAccountDiscount = new MaAccountDiscount();

		ResultSupport result = BeanUtilsExtends.copy(maAccountDiscount, maAccountDiscountDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		maAccountDiscount.setCreatetime(Calendar.getInstance().getTime());
		maAccountDiscount.setIsDelete(1);
		int r = -1;

		try {
			r = accountDiscountMapper.insert(maAccountDiscount);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, maAccountDiscount.getId());
		return result;
	}

	public ResultDO modifi(AccountDiscountDo maAccountDiscountDo) {
		MaAccountDiscount maAccountDiscount = new MaAccountDiscount();

		ResultSupport result = BeanUtilsExtends.copy(maAccountDiscount, maAccountDiscountDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = accountDiscountMapper.updateByPrimaryKeySelective(maAccountDiscount);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaAccountDiscount maAccountDiscount = new MaAccountDiscount();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		int r = -1;
		maAccountDiscount.setId(id);
		maAccountDiscount.setIsDelete(-1);
		try {
			r = accountDiscountMapper.updateByPrimaryKeySelective(maAccountDiscount);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		MaAccountDiscount accountDiscount = null;

		try {
			accountDiscount = accountDiscountMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		if (accountDiscount == null) {
			result.setErrorCode(ResultDO.ACCOUNTDISCOUNT_GET_ERROR);
			result.setErrorMsg("不存在该会员优惠卷信息");
			result.setSuccess(false);
			return result;
		}

		AccountDiscountDo maAccountDiscountDo = getMaAccountDiscountDo(accountDiscount);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maAccountDiscountDo);
		return result;
	}

	public ResultDO query(AccountDiscountQuery maAccountDiscountQuery) {

		ResultSupport result = new ResultSupport();

		List<MaAccountDiscount> list = null;

		MaAccountDiscountExample example = new MaAccountDiscountExample();

		Criteria createCriteria = example.createCriteria();

		if (StringUtils.isNotBlank(maAccountDiscountQuery.getName())) {
			createCriteria.andNameEqualTo(maAccountDiscountQuery.getName());
		}

		if (maAccountDiscountQuery.getMemberId() != null) {
			createCriteria.andMemberIdEqualTo(maAccountDiscountQuery.getMemberId());
		}

		if (maAccountDiscountQuery.getUserType() != null) {
			createCriteria.andUserTypeEqualTo(maAccountDiscountQuery.getUserType());
		}

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (StringUtils.isNotBlank(maAccountDiscountQuery.getValidity())) {
				createCriteria.andValidityLessThanOrEqualTo(sdf.parse(maAccountDiscountQuery.getValidity()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		createCriteria.andIsDeleteEqualTo(1);
		List<AccountDiscountDo> accountDiscountDos = null;

		if (maAccountDiscountQuery.isPage()) {
			example.setLimitByPage(maAccountDiscountQuery.getStart() + "," + maAccountDiscountQuery.getPageRows());
			long count = accountDiscountMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = accountDiscountMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取会员优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		accountDiscountDos = getMaAccountDiscountDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, accountDiscountDos);
		return result;
	}

	@Override
	public Integer count(AccountDiscountQuery accountDiscountQuery) {

		MaAccountDiscountExample example = new MaAccountDiscountExample();

		Criteria createCriteria = example.createCriteria();

		if (accountDiscountQuery.getMemberId() != null) {

			createCriteria.andMemberIdEqualTo(accountDiscountQuery.getMemberId());
		}

		if (accountDiscountQuery.getUserType() != null) {

			createCriteria.andUserTypeEqualTo(accountDiscountQuery.getUserType());
		}

		createCriteria.andIsDeleteEqualTo(1);
		Integer count = accountDiscountMapper.countByExample(example);
		return count;
	}

}
